/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Default menu layer for testing purposes
 */

#include "MenuLayer.h"
#include "MenuScene.h"
#include "../MainEngine.h"
#include "../Common.h"
#include "../Resources.h"
#include "../editor/EditorScene.h"
#include "../game/GameScene.h"
#include "../misc/UserData.h"

using namespace cocos2d;

// default constructor
MenuLayer::MenuLayer( MenuScene *scene ) : mMaster( scene ), mMainMenu( 0 ), mMenuUnitSprite( 0 ), mBackground( 0 ) {
	initLayer();
}

// default destructor
MenuLayer::~MenuLayer() {}

// initializes this layer
void MenuLayer::initLayer() {
	// disable input
	setIsTouchEnabled( false );

	// init background
	mBackground = cocos2d::CCSprite::spriteWithFile( getResource( Textures::GAME_BACKGROUND ) );
	mBackground->setScaleX( RATEX );
	mBackground->setScaleY( RATEY );
	mBackground->setPosition( ccp( WIN_WIDTH * 0.5f, WIN_HEIGHT * 0.5f ) );
	addChild( mBackground );

	// create unit sprite
	mMenuUnitSprite = CCSprite::spriteWithFile( getResource( Textures::MENU_UNIT ) );
	mMenuUnitSprite->setPosition( ccp( WIN_WIDTH * 0.15f, WIN_HEIGHT * 0.4f ) );
	mMenuUnitSprite->setScaleX( RATEX * 0.9 );
	mMenuUnitSprite->setScaleY( RATEY * 0.9 );
	addChild( mMenuUnitSprite );

	// create menu
	mMainMenu = CCMenu::node();
	mMainMenu->setPosition( CCPointZero );
	addChild( mMainMenu );

	// game title
	CCLabelBMFont *titleLabel1 = CCLabelBMFont::labelWithString( "Demolition Expert", getResource( Fonts::FONT_NORMAL ) );
	titleLabel1->setPosition( WIN_WIDTH * 0.5f, WIN_HEIGHT * 0.88f );
	titleLabel1->setScale( RATEX * 1.7 );
	addChild( titleLabel1 );

	// exit button
	CCLabelBMFont *exitLabel1 = CCLabelBMFont::labelWithString( "Exit", getResource( Fonts::FONT_NORMAL ) );
	CCMenuItemLabel *exitLabel = CCMenuItemLabel::itemWithLabel( exitLabel1, this, menu_selector( MenuLayer::exitButtonCallback ) );
	exitLabel->setPosition( WIN_WIDTH * 0.9f, WIN_HEIGHT * 0.1f );
	exitLabel->setScale( RATEX * 0.57f );
	mMainMenu->addChild( exitLabel, 0 );
	
	// editor button
	CCLabelBMFont *editorLabel1 = CCLabelBMFont::labelWithString( "Level\nEditor", getResource( Fonts::FONT_NORMAL ) );
	CCMenuItemLabel *editorLabel = CCMenuItemLabel::itemWithLabel( editorLabel1, this, menu_selector( MenuLayer::launchEditorCallback ) );
	editorLabel->setPosition( WIN_WIDTH * 0.6f, WIN_HEIGHT * 0.1f );
	editorLabel->setScale( RATEX * 0.70f );
	mMainMenu->addChild( editorLabel, 0 );

	// money label
	unsigned int moneyAsNumber = mMaster->getMaster()->getUserData()->getGameData()->currentMoney;
	stringstream sstream;
	sstream << moneyAsNumber;
	string moneyAsString = sstream.str();
	moneyAsString = moneyAsString + "$";
	CCLabelBMFont *moneyLabel = CCLabelBMFont::labelWithString( moneyAsString.c_str() , Fonts::FONT_NORMAL );
	moneyLabel->setPosition( WIN_WIDTH * 0.90f, WIN_HEIGHT * 0.6f );
	moneyLabel->setScale( RATEX * 0.70f );
	addChild( moneyLabel, 0 );
	
	initLevelButtons();
}

// init level buttons
void MenuLayer::initLevelButtons() 
{
	// create buttons
	for( unsigned int i = 0; i < 10; ++i ) {
		// button itself
		CCMenuItemImage *levelButton = CCMenuItemImage::itemFromNormalImage( getResource( Textures::MENU_LEVEL_BUTTON ), getResource( Textures::MENU_LEVEL_BUTTON_SELECTED ), this, menu_selector( MenuLayer::launchGameCallback ) );
		levelButton->setUserData( new int( i + 1 ) );
		levelButton->setScale( RATEX * 1.35f );
		levelButton->setPosition( WIN_WIDTH * 0.3f + WIN_WIDTH * 0.1f * i - ( i > 4 ? WIN_WIDTH * 0.5f : 0 ), WIN_HEIGHT * 0.6f - ( i > 4 ? WIN_HEIGHT * 0.2f : 0 ) );

		// text above it
		char leveli[ 3 ];
		sprintf( leveli, "%d", ( i + 1 ) );
		CCLabelBMFont *levelLabel = CCLabelBMFont::labelWithString( leveli, getResource( Fonts::FONT_NORMAL ) );
		levelLabel->setScale( RATEX * 0.7f );
		levelLabel->setPosition( levelButton->getPosition() );

		// possible lock sprite
		if( i > 0 && mMaster->getMaster()->getUserData()->getGameData()->starsCollected[ i - 1 ] == 0 ) {
			CCSprite *lockSprite = CCSprite::spriteWithFile( getResource( Textures::MENU_LEVEL_LOCK ) );
			lockSprite->setScale( RATEX * 0.5f );
			lockSprite->setAnchorPoint( ccp( 0.5f, 0.8f ) );
			lockSprite->setPosition( ccp( levelButton->getPosition().x, levelButton->getPosition().y + WIN_HEIGHT * 0.03f ) );
			levelButton->setIsEnabled( false );
			addChild( lockSprite, 2 );

			// cool lock animation
			float time = ( rand() % 100 ) / 400.0f + 0.7f;
			CCActionInterval *a1 = CCRotateTo::actionWithDuration( time, 30.0f );
			CCActionInterval *a2 = CCRotateTo::actionWithDuration( time, -30.0f );
			CCFiniteTimeAction *e1 = cocos2d::CCEaseInOut::actionWithAction( a1, 2.0f );
			CCFiniteTimeAction *e2 = cocos2d::CCEaseInOut::actionWithAction( a2, 2.0f );
			CCSequence *s = CCSequence::actionOneTwo( e1, e2 );
			lockSprite->runAction( CCRepeatForever::actionWithAction( s ) );
		}

		// add everything to layer
		mMainMenu->addChild( levelButton, 0 );
		addChild( levelLabel, 1 );
	}

	// custom level
	{
		CCMenuItemImage *levelButton = CCMenuItemImage::itemFromNormalImage( getResource( Textures::MENU_LEVEL_BUTTON ), getResource( Textures::MENU_LEVEL_BUTTON_SELECTED ), this, menu_selector( MenuLayer::launchGameCallback ) );
		levelButton->setUserData( new int( 0 ) );
		levelButton->setScale( RATEX * 1.35f );
		levelButton->setPosition( WIN_WIDTH * 0.4f, WIN_HEIGHT * 0.1f );
		CCLabelBMFont *levelLabel = CCLabelBMFont::labelWithString( "*", getResource( Fonts::FONT_NORMAL ) );
		levelLabel->setScale( RATEX * 0.7f );
		levelLabel->setPosition( levelButton->getPosition() );
		mMainMenu->addChild( levelButton, 0 );
		addChild( levelLabel, 1 );
	}
}

// update the layer
void MenuLayer::update( cocos2d::ccTime dt ) {
	// TODO
}

// exit button callback
void MenuLayer::exitButtonCallback( cocos2d::CCObject *pSender ) {
	// close the window
	CCDirector::sharedDirector()->end();
}

// launch editor callback
void MenuLayer::launchEditorCallback( cocos2d::CCObject *pSender ) {
	cocos2d::CCScene *editorScene = new EditorScene( mMaster->getMaster() );
	mMaster->getMaster()->sceneFinishedExecution( editorScene, SCENE_EDITOR );
}

// launch game callback
void MenuLayer::launchGameCallback( cocos2d::CCObject *pSender ) {
	int level = *( static_cast< int* >( dynamic_cast< CCMenuItemSprite* >( pSender )->getUserData() ) );
	cocos2d::CCScene *editorScene = new GameScene( mMaster->getMaster(), level );
	mMaster->getMaster()->sceneFinishedExecution( editorScene, SCENE_EDITOR );
}
